package cn.alone.org.service.impl;

import cn.alone.basic.BusinessException;
import cn.alone.basic.service.impl.BaseServiceImpl;
import cn.alone.org.domain.Employee;
import cn.alone.org.domain.Shop;
import cn.alone.org.mapper.EmployeeMapper;
import cn.alone.org.mapper.ShopMapper;
import cn.alone.org.service.IEmployeeService;
import cn.alone.org.service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 部门业务实现层
 * @author lanan
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private IEmployeeService employeeService;

    @Autowired
    private EmployeeMapper employeeMapper;


  /**
   * 批量删除
   * @param ids
   */
    @Override
    public void patchDelete(List<Long> ids) {
        shopMapper.batchDeletion(ids);
    }

    //查询一二级树形部门数据
    @Override
    public List<Shop> findTree() {
        return shopMapper.findOneAndTwo();
    }


    /**
     * 商家入驻
     * @param shop
     */
    @Override
    public void settledIn(Shop shop) {
        /*校验*/
        Employee admin = shop.getAdmin();
        /* 1.校验注册时输入的两次密码是否一致*/
        if(!admin.getPassword().equals(admin.getComfirmPassword())){
            /*这里用自定义异常让controller捕捉*/
            throw new BusinessException("注册密码不一致，请重新输入");
        }
        /*2.根据用户传输过来的注册信息对比数据库的username,phone,emali*/
        Employee emp = employeeMapper.loadByUsername(admin.getUsername());
        if (emp!=null){
            throw new BusinessException("账户已存在请直接登录！！");
        }
        /**
         * 注册逻辑
         * shop表格中admin_id对应emp表格id
         *emp表格中的shop_id对应shop的id
         *
         */
       /* 保存emp拿到emp自增长id*/
        employeeService.add(admin);
       /* 保存shop拿到自增长id*/
        shop.setAdmin(admin);
      /*  保存shop*/
        shopMapper.save(shop);
      /*  给admi中的shopid赋值*/
        admin.setShop_id(shop.getId());
      /*  修改保存emp表格*/
        employeeMapper.update(admin);




    }


}
